using System;
using System.Linq;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Chaks.SharePoint.Docs.ILP.Utilities;

namespace Chaks.SharePoint.Docs.ILP.EventReceivers
{
    [System.ComponentModel.Category("90D11665-80CA-4DEF-88BF-3D73B542CDEB")] //Don't remove! This attribute is added by SPVisualDev to bind it to a WSS feature ID.
    public class ILPFeatureEventReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// Initializes a new instance of the Microsoft.SharePoint.SPFeatureReceiver class.
        /// </summary>
        public ILPFeatureEventReceiver()
        {
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="properties">
        /// A Microsoft.SharePoint.SPFeatureReceiverProperties object that represents properties of the event handler.
        /// </param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="properties">
        /// A Microsoft.SharePoint.SPFeatureReceiverProperties object that represents properties of the event handler.
        /// </param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPWeb web = properties.GetSite().RootWeb)
            {
                // Create the List
                web.CreateList(
                    SPListTemplateType.GenericList,
                    "ILPSet",
                    "List to store Item-level Permission sets",
                    null, true, false, true, false, true);

                // Assign the appropriate Content Type to the List
                web.AssignContentTypeToList("ILPSet", "Item-level Permission Item");

                // Get the List
                SPList ilpList = web.Lists["ILPSet"];

                // Assign some basic properties for the List
                ilpList.EnableAttachments = false;
                ilpList.EnableVersioning = true;
                ilpList.OnQuickLaunch = false;
                ilpList.NoCrawl = false;
                
                // Update the List
                ilpList.Update();
            }
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="properties">
        /// A Microsoft.SharePoint.SPFeatureReceiverProperties object that represents properties of the event handler.
        /// </param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPWeb web = properties.GetSite().RootWeb)
            {
                web.Lists["ILPSet"].Delete();
            }
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="properties">
        /// A Microsoft.SharePoint.SPFeatureReceiverProperties object that represents properties of the event handler.
        /// </param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}
